var addrbill = require('../../../sys/sys_addr.js')
var locationhelper = require('../../../sys/locationhelper')
Component({
  options: {
    addGlobalClass: true,
    multipleSlots: true
  },
  /**
   * 组件的属性列表
   */
  properties: {
    navCur: {
      type: String,
      value: "0"
    },
    themcolor: {
      type: Object,
      default: {}
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    op: "",
    plactetxt:"定位中",
    delivery_config: { openloacion: -1 },
    addrmodel: {}
  },
  ready() {
    var app = getApp();
    var addrmodel = {}
    if (app.globalData.member) {
      addrmodel["phone"] = app.globalData.member["phone"]
      if (app.globalData.member.memberinfo) {
        var memberinfo = app.globalData.member.memberinfo;
        addrmodel["name"] = memberinfo["name"]

      }

      this.setData({ addrmodel })
    }
    var delivery_config = app.globalData.configdata["delivery_config"]
    this.setData({ delivery_config })
    this.loadaddr();
  },

  /**
   * 组件的方法列表
   */
  methods: {


    fieldchange(ev){
     
      this.data.addmodel[ev.currentTarget.dataset.field] =ev.detail
// mark: xxx
      this.triggerEvent("change", this.data.addmodel,{update:false})
    },
    getaddrmodel(){

    return this.data.addmodel;
    },

    loadaddr() {
      var that = this;
      locationhelper.getcity().then(function (resdata) {  
      
        //plactetxt
        var detail = resdata.addr
        if(detail && detail['status']==-1){
          wx.showToast({
            icon:"none",
            title: detail['message'],
          })
          return false;
        }
        addrbill.wxaddrtosysaddr({ cityName: detail.ad_info.city, countyName: detail.ad_info.district, provinceName: detail.ad_info.province }).then(function (myaddr) {

          var addrmodel = that.data.addrmodel
          addrmodel["province"] = detail.ad_info.province;
          addrmodel["city"] = detail.ad_info.city;
          addrmodel["county"] = detail.ad_info.district;
          addrmodel["lat"] = detail.location["lat"]
          addrmodel["lng"] = detail.location["lng"]
          addrmodel["addrinfo"] = detail.address
          addrmodel["addrname"] = detail.name

          if (myaddr) {
            var ilen = myaddr.length
            if (myaddr.length < 3) {
              for (; ilen < 3; ilen++) {
                myaddr.push({ id: "", name: "" })
              }
            }
            addrmodel["region"] = myaddr;
            addrmodel["provincecode"] = myaddr[0]["id"];
            addrmodel["citycode"] = myaddr[1]["id"];
            addrmodel["countycode"] = myaddr[2]["id"];

            that.setData({ addrmodel })
          }


        })
      })
    },
    showop(op, addmodel) {
      var updatemodel = {}
      updatemodel["op"] = op;
      if (!addmodel) {
        addmodel = this.clearmodel(true)
      }
      updatemodel["addrmodel"] = addmodel
      this.setData(updatemodel)
    },
    hideform() {
      this.setData({ op: '' })

    },
    changeisdefault() {
      var upatemodel = {};
      upatemodel["addrmodel.isdefault"] = -this.data.addrmodel.isdefault
      this.setData(upatemodel)
    },
    chooseLocation(ev) {
    
      var mydata = ev;
      

      var that = this;
      addrbill.wxaddrtosysaddr({ cityName: mydata.city, countyName: mydata.district, provinceName: mydata.province }).then(function (myaddr) {

        var addrmodel = that.data.addrmodel
        addrmodel["province"] = mydata.province;
        addrmodel["city"] = mydata.city;
        addrmodel["county"] = mydata.district;


        if (myaddr) {
          var ilen = myaddr.length
          if (myaddr.length < 3) {
            for (; ilen < 3; ilen++) {
              myaddr.push({ id: "", name: "" })
            }
          }
          addrmodel["region"] = myaddr;
          addrmodel["provincecode"] = myaddr[0]["id"];
          addrmodel["citycode"] = myaddr[1]["id"];
          addrmodel["countycode"] = myaddr[2]["id"];
        }
        addrmodel.lat = mydata["latitude"];
        addrmodel.lng = mydata["longitude"];
        addrmodel.addrname = mydata["name"];
        addrmodel.addrinfo = mydata["address"];
        that.setData({ addrmodel })
      })

    },
    chosepostion: function (e) {


      var delivery_config = this.data.delivery_config
      if (delivery_config.loaciontype == 1) {
        var qqstylemap = this.selectComponent("#qqstylemap")
        var mode = 1;
        if (this.data.navCur == 3) {
          //域名限制
          mode = 2
        }

        var addrmodel = this.data.addrmodel
        qqstylemap.showmap(mode, addrmodel, delivery_config.intracity)
        return;
      }      

    },
    chooseaddress: function (e) {
      var data = e.detail;

      this.setData({
        "addrmodel.region": data
      })
    },
    //选择地区
    pickerblock: function () {
      this.selectComponent("#picker").setinitdata();
    },
    //地址提交
    getaddrmodel() {
      var addrmodel = this.data.addrmodel;   
      if (this.data.navCur == 3) {

        if (addrmodel.lat == 0 || !addrmodel.lat) {
          wx.showToast({
            title: '请选择地址',
          })

          return
        }
      }

   
      var vdata = {
        "name": ["", "收货人姓名不能为空"],
        "phone": ["", "手机不能为空"],
        "region": ["", "请选择地址"],
        "addrinfo": ["", "详细地址不能为空"]
      }
      var errors = [];
      for (var a in vdata) {
        if (!addrmodel[a] || addrmodel[a] == vdata[a][0]) {
          errors.push(vdata[a][1])
        }
      }

      if (errors.length > 0) {
        wx.showModal({
          title: '错误',
          content: errors.join("\r\n"),
        })
        return false;
      }
      else {
        addrmodel.province = addrmodel.region[0].name;
        addrmodel.provincecode = addrmodel.region[0].id;
        addrmodel.city = addrmodel.region[1].name;
        addrmodel.citycode = addrmodel.region[1].id;
        addrmodel.county = addrmodel.region[2].name;
        addrmodel.countycode = addrmodel.region[2].id;
        if ((!addrmodel.provincecode) || (!addrmodel.countycode) || (!addrmodel.citycode)) {
          wx.showModal({
            title: '错误',
            content: "请选择地区",
          })
          return false;
        }

        return addrmodel;
        
      }
    },
    //获取手机号
    getPhoneNumber(e) {
      var that = this;
      var dataset = e.currentTarget.dataset;
      var encryptedData = e.detail.encryptedData;
      var iv = e.detail.iv;
      var code = e.detail.code;

      addrbill.getphone(encryptedData, iv,code).then(function (data) {
        var upatemodel = {};
        if (data["code"] == "success") {
          upatemodel["addrmodel.phone"] = data["obj"].phoneNumber;
        }
        that.setData(upatemodel);

      });

    },
    selectedaddr(ev) {
      var detail = ev.detail
      var that = this;
      addrbill.wxaddrtosysaddr({ cityName: detail.ad_info.city, countyName: detail.ad_info.district, provinceName: detail.ad_info.province }).then(function (myaddr) {

        var addrmodel = that.data.addrmodel
        if(  addrmodel["addrinfo"])
        {
          return ;
        }
        addrmodel["province"] = detail.ad_info.province;
        addrmodel["city"] = detail.ad_info.city;
        addrmodel["county"] = detail.ad_info.district;
        addrmodel["lat"] = detail.location["lat"]
        addrmodel["lng"] = detail.location["lng"]
        addrmodel["addrinfo"] = detail.address
        addrmodel["addrname"] = detail.title

        if (myaddr) {
          var ilen = myaddr.length
          if (myaddr.length < 3) {
            for (; ilen < 3; ilen++) {
              myaddr.push({ id: "", name: "" })
            }
          }
          addrmodel["region"] = myaddr;
          addrmodel["provincecode"] = myaddr[0]["id"];
          addrmodel["citycode"] = myaddr[1]["id"];
          addrmodel["countycode"] = myaddr[2]["id"];
        }


        that.setData({ addrmodel })
      })




    },
    //清空数据
    clearmodel(black) {
      var clearupdate = {
        addrmodel: {
          isdefault: "-1",
          addrinfo: "",
          region: [],
          phone: "",
          name: ""
        }
      }
      if (black) {
        return clearupdate.addrmodel
      }
      else {
        this.setData(clearupdate)
      }
    }
  }
})
